<template>
  <div class="comment-reply">
    <van-nav-bar
      :title="
        comment.reply_count > 0 ? `${comment.reply_count}条回复` : '暂无回复'
      "
    >
      <van-icon slot="left" name="cross" @click="$emit('close')" />
    </van-nav-bar>
    <div class="scroll-wrap">
      <!-- 当前评论调用评论项模板,把当前评论项传递过去 -->
      <CommentItem :comment="comment" />
      <van-cell title="全部回复" />
      <!-- 当前评论的全部回复列表 要传递当前评论项的id  评论项的评论type值是c  commentsList是把评论完之后的数据返回到评论列表中,再返回到主页中进行渲染-->
      <CommentList type="c" :source="comment.com_id" :list="commentsList" />
      <!-- 回复评论 -->
      <!-- 回复评论按钮 -->
      <div class="post-wrap">
        <van-button
          class="post-btn"
          size="small"
          round
          @click="isPostShow = true"
          >写评论</van-button
        >
      </div>
      <van-popup v-model="isPostShow" position="bottom">
        <CommentPost :target="comment.com_id" @commentSuccess="oncommentList" />
      </van-popup>
    </div>
  </div>
</template>

<script>
import CommentItem from './comment-item.vue'
import CommentList from './comment-list.vue'
import CommentPost from './comment-post.vue'
export default {
  name: 'CommentReply',
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  components: {
    CommentItem,
    CommentList,
    CommentPost
  },
  data() {
    return {
      isPostShow: false,
      commentsList: []
    }
  },
  methods: {
    oncommentList(data) {
      this.$toast.loading({
        message: '发布中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间，默认 2000，0 表示持续展示不关闭
      })
      this.comment.reply_count++
      this.isPostShow = false
      this.commentsList.unshift(data.new_obj)
      this.$toast.success('发布成功')
    }
  }
}
</script>

<style lang="less" scoped>
.scroll-wrap {
  position: fixed;
  top: 190px;
  right: 0;
  bottom: 88px;
  left: 0;
  overflow-y: auto;
}
.post-wrap {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  height: 88px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border-top: 1px solid #d8d8d8;
  .post-btn {
    width: 60%;
  }
}
</style>
